package com.beater.controller.bean;

import com.beater.controller.code.ResponseCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
* 封装后端返回的Json数据
*/
//保证序列化json时，如果是null的对象，key也会消失
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseBodyJson<T> {
	
	private String returnCode ;
	private T data ;
	private String returnMessage ;
	
	//使之不在序列化结果中
	@JsonIgnore
	public boolean isSuccess(){
		return this.returnCode == ResponseCode.SUCCESS.getCode() ;
	}
	
	/**
	 * 
	 * @param msg
	 * @param data
	 * @return 
	 */
	public static <T>ResponseBodyJson<T> createSuccessResBody(String msg,T data){
		return new ResponseBodyJson<T>(ResponseCode.SUCCESS.getCode(), data, msg) ;
	}
	
	public static <T>ResponseBodyJson<T> createLoginSuccessResBody(T data){
		return new ResponseBodyJson<T>(ResponseCode.SUCCESS_LOGIN.getCode(), data, ResponseCode.SUCCESS_LOGIN.getDesc()) ;
	}
	/**
	 * 
	 * @param msg
	 * @return
	 */
	public static <T>ResponseBodyJson<T> createErrorResBody(String msg) {
		return new ResponseBodyJson<T>(ResponseCode.ERROR.getCode(), null, msg) ;
	}
	
	public static <T>ResponseBodyJson<T> createTokenErrorResBody() {
		return new ResponseBodyJson<T>(ResponseCode.AUTH_TOKEN_ERROR.getCode(), null, ResponseCode.AUTH_TOKEN_ERROR.getDesc()) ;
	}
	public static <T>ResponseBodyJson<T> createParamErrorResBody() {
		return new ResponseBodyJson<T>(ResponseCode.ERROR_PARAM.getCode(), null, ResponseCode.ERROR_PARAM.getDesc()) ;
	}
	/**
	 * 
	 * @param msg
	 * @return
	 */
	public static <T>ResponseBodyJson<T> createErrorResBody(String msg,String code) {
		return new ResponseBodyJson<T>(code, null, msg) ;
	}
	
	public ResponseBodyJson(String returnCode, T data, String returnMessage) {
		super();
		this.returnCode = returnCode;
		this.data = data;
		this.returnMessage = returnMessage;
	}



	public String getReturnCode() {
		return returnCode;
	}



	public void setReturnCode(String returnCode) {
		this.returnCode = returnCode;
	}



	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	public String getReturnMessage() {
		return returnMessage;
	}
	public void setReturnMessage(String returnMessage) {
		this.returnMessage = returnMessage;
	}
	
	
	

}
